var UserData = require('UserData')
var users = require('users')
cc.Class({
    extends: cc.Component,

    properties: {
        promptPrefab:{
            default:null,
            type:cc.Prefab
        },
        sideButton:{
            default:null,
            type:cc.Node
        },
        multiplierButton:{
            default:null,
            type:cc.Node
        },
        overTimeSkill:{
            default:null,
            type:cc.Node
        },
        fortuneCatSkill:{
            default:null,
            type:cc.Node
        },
        earthShakeSkill:{
            default:null,
            type:cc.Node
        },
        rankButton:{
            default:null,
            type:cc.Node
        },
        chefNode:{
            default:null,
            type:cc.Node
        },
        recipeNode:{
            default:null,
            type:cc.Node
        },
        sellBuildingNode:{
            default:null,
            type:cc.Node
        },
        secretaryNode:{
            default:null,
            type:cc.Node
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

        this.getComponent()
        this.arrowDirection = "down"
        this.tutorialStep = -1
        this.TutorialArray= {
            welcome:{
                stepList:[
                {
                    button:null,
                    description:"欢迎来到纸片餐厅~"
                },
                {
                    button:null,
                    description:"我是您的小秘书，安娜。"
                },
                {
                    button:null,
                    description:"老板来点击第一层的绿色按钮开启自己的第一家餐厅吧。"
                },

            ]},

            chefNode:{
                button:this.chefNode,
                direction:0,
                distance:cc.v2(0,480),
                description:"这里可以查看到主厨和客人的信息哦~"
            },
            recipeNode:{
                button:this.recipeNode,
                direction:0,
                distance:cc.v2(0,480),
                description:"购买食谱和食材，提升收益吧！"
            },
            sellBuildingNode:{
                button:this.sellBuildingNode,
                direction:0,
                distance:cc.v2(0,480),
                description:"这里有一个加速升级的方法哦。"
            },
            secretaryNode:{
                button:this.secretaryNode,
                direction:0,
                distance:cc.v2(480,0),
                description:"之后可以在这里联系我~"
            },
            sideButton:{
                    button:this.sideButton,
                    direction:0,
                    distance:cc.v2(0,480),
                    description:"这里可以展开侧边栏~"
            },
            multiplierButton:{
                button:this.multiplierButton,
                description:"这里可以修改一次升级的等级数。"
            },
            overTimeSkill:{
                button:this.overTimeSkill,
                description:"点击可以让使得接下来的时间所有餐厅都处于双倍收益状态哦。"
            },
            fortuneCatSkill:{
                button:this.fortuneCatSkill,
                description:"招财猫可以快速招揽一批客人。"
            },
            earthShakeSkill:{
                button:this.earthShakeSkill,
                description:"点击这里可以触发地震哦（摇晃手机也可以）。"
            },
            rankButton:{
                button:this.rankButton,
                description:"查看好友的排名吧。"
            }
        }
        this.guide = this.TutorialArray.welcome
        //this.next()
    },
    next(target){
        this.node.active = true
            if(this.guide.stepList){
                this.tutorialStep+=1
                if(this.tutorialStep>this.guide.stepList.length){
                    this.endTutorial()
                    return
                }
                if(this.guide.stepList[this.tutorialStep]){//
                    if(this.guide.stepList[this.tutorialStep].button){//有button的情况下点击小秘书不做任何变化
                        this.anchorPos(this.guide.stepList[this.tutorialStep])
                        return
                    }else{//没有button的情况下点击任意地方都进行下一步
                        this.arrow.active =false
                        if(this.tutorialStep>=this.guide.stepList.length){
                            this.endTutorial()
                        }else{
                            this.label.string = this.guide.stepList[this.tutorialStep].description
                        }
                        return
                    }
                }
            }else{//单步教程没有list
                if(!this.guide.played){
                    this.guide.played = true
                    if(this.guide.button){
                        this.anchorPos(this.guide)
                        this.label.string = this.guide.description
                    }else{
                        this.endTutorial()
                    }
                }
            }
    },
    endTutorial(){
        try{
            this.buttonCopy.destroy()
        }catch(err){

        }
        switch(this.guide){
            case this.TutorialArray.welcome:
                UserData.runtimeState.tutorialProgress.welcome = true
            break
            case this.TutorialArray.chefNode:
                UserData.runtimeState.tutorialProgress.chefNode = true
            break
            case this.TutorialArray.recipeNode:
                UserData.runtimeState.tutorialProgress.recipeNode = true
            break
            case this.TutorialArray.sellBuildingNode:
                UserData.runtimeState.tutorialProgress.sellBuildingNode = true
            break
            case this.TutorialArray.sideButton:
                UserData.runtimeState.tutorialProgress.sideButton = true
            break
        }
        users.setUser({tutorial:UserData.runtimeState.tutorialProgress})
        this.node.active =false//结束教程
    },
    dialog(){
        this.TutorialArray[this.tutorialStep]
    },
    anchorPos(guide){
        if(guide.button == this.sideButton){
            this.sideButtonTutorial = true
            //添加提示框
            let sideButtonList = [
                this.TutorialArray.multiplierButton,
                this.TutorialArray.overTimeSkill,
                this.TutorialArray.fortuneCatSkill,
                this.TutorialArray.earthShakeSkill,
                this.TutorialArray.rankButton
            ]
            for(let i=0;i<sideButtonList.length;i++){
                let prompt = cc.instantiate(this.promptPrefab)
                prompt.getChildByName("lines").getComponent(cc.Label).string = sideButtonList[i].description
                sideButtonList[i].button.insertChild(prompt)
            }
        }
        if(guide.description){
            this.label.string = guide.description
        }
        let node = guide.button
        let direction = guide.direction
        let distance = guide.distance
        if(!node){
            this.arrow.active = false
        }else{
            this.arrow.active = true
            
            let worldPos = node.parent.convertToWorldSpaceAR(node.getPosition())
            let nodePos = this.node.convertToNodeSpaceAR(worldPos)
            this.arrow.x = nodePos.x + distance.x
            this.arrow.y = nodePos.y + distance.y
            this.arrow.rotation = direction

            this.buttonCopy = cc.instantiate(node)
            this.buttonCopy.parent = this.node
            this.buttonCopy.position = nodePos
            this.buttonCopy.on(cc.Node.EventType.TOUCH_END,()=>{
                    this.endTutorial()
            })
            this.arrow.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.3,cc.v2(0,-50)),cc.moveBy(1,cc.v2(0,50)))))
        }
    },
    getComponent(){
        this.prompt = this.node.getChildByName("提示")
        this.arrow = this.node.getChildByName("箭头")
        this.label = this.node.getChildByName("提示").getChildByName("内容").getComponent(cc.Label)
        this.windowSize = cc.view.getCanvasSize()
    },
    traspanrentBackGround(){
        this.node.getComponent(cc.Sprite).active =false
    },
    maskBackGround(){
        this.node.getComponent(cc.Sprite).active =true
    },

    update (dt) {
        if(this.sideButtonTutorial){
            //添加侧边栏
        }
    },
});
